package com.zheng.umbrella.web.controller.manage;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.zheng.common.base.BaseController;
import com.zheng.common.validator.LengthValidator;
import com.zheng.umbrella.common.constant.UmbrellaResult;
import com.zheng.umbrella.common.constant.UmbrellaResultConstant;
import com.zheng.umbrella.dao.model.UmbrellaRuleSpecialconfig;
import com.zheng.umbrella.dao.model.UmbrellaRuleSpecialconfigExample;
import com.zheng.umbrella.rpc.api.UmbrellaRuleSpecialconfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 个性规则配置controller
 * Created by shuzheng on 2017/2/6.
 */
@Controller
@Api(value = "个性规则配置管理", description = "个性规则配置管理")
@RequestMapping("/manage/specialconfig")
public class UmbrellaRuleSpecialConfigController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UmbrellaRuleSpecialConfigController.class);

    @Autowired
    private UmbrellaRuleSpecialconfigService umbrellaRuleSpecialconfigService;

    @ApiOperation(value = "个性规则配置首页")
    @RequiresPermissions("umbrella:specialconfig:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/specialconfig/index.jsp";
    }

    @ApiOperation(value = "个性规则配置列表")
    @RequiresPermissions("umbrella:specialconfig:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        UmbrellaRuleSpecialconfigExample umbrellaRuleSpecialconfigExample = new UmbrellaRuleSpecialconfigExample();
        UmbrellaRuleSpecialconfigExample.Criteria criteria = umbrellaRuleSpecialconfigExample.createCriteria();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            umbrellaRuleSpecialconfigExample.setOrderByClause(sort + " " + order);
        }else{
            umbrellaRuleSpecialconfigExample.setOrderByClause(" id asc");
        }
        if (StringUtils.isNotBlank(search)) {
            criteria.andNameLike("%" + search + "%");
        }
        List<UmbrellaRuleSpecialconfig> rows = umbrellaRuleSpecialconfigService.selectByExampleForOffsetPage(umbrellaRuleSpecialconfigExample, offset, limit);
        long total = umbrellaRuleSpecialconfigService.countByExample(umbrellaRuleSpecialconfigExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增个性规则配置")
    @RequiresPermissions("umbrella:specialconfig:create")
    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create() {
        return "/manage/specialconfig/create.jsp";
    }

    @ApiOperation(value = "新增个性规则配置")
    @RequiresPermissions("umbrella:specialconfig:create")
    @ResponseBody
    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    public Object create(UmbrellaRuleSpecialconfig umbrellaRuleSpecialconfig) {
        int count = umbrellaRuleSpecialconfigService.insertSelective(umbrellaRuleSpecialconfig);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除个性规则配置")
    @RequiresPermissions("umbrella:specialconfig:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
//        int count = umbrellaRuleSpecialconfigService.deleteByPrimaryKeys(ids);
        int count = umbrellaRuleSpecialconfigService.deleteLogicByPrimaryKeys(ids,UmbrellaRuleSpecialconfig.class,null);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改个性规则配置")
    @RequiresPermissions("umbrella:specialconfig:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET})
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        UmbrellaRuleSpecialconfig umbrellaRuleSpecialconfig = umbrellaRuleSpecialconfigService.selectByPrimaryKey(id);
        modelMap.put("umbrellaRuleSpecialconfig", umbrellaRuleSpecialconfig);
        return "/manage/specialconfig/update.jsp";
    }

    @ApiOperation(value = "修改个性规则配置")
    @RequiresPermissions("umbrella:specialconfig:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public Object update(@PathVariable("id") int id, UmbrellaRuleSpecialconfig umbrellaRuleSpecialconfig) {;
        int count = umbrellaRuleSpecialconfigService.updateByPrimaryKeySelective(umbrellaRuleSpecialconfig);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }
}
